package com.happyfamily.springboot.crowdsourcingplatform.controller.weng;


import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dto.RoleDto;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysRole;
import com.happyfamily.springboot.crowdsourcingplatform.service.weng.RoleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/api-back/role")
@Slf4j
public class RoleController
{

	@Autowired
	private RoleService roleService;


	@GetMapping("/all")
	@ResponseBody
	@ApiOperation(value = "后台获取所有角色", notes = "后台获取所有角色", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "后台获取所有角色", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public Results<SysRole> getAll()
	{
		return roleService.getAllRoles();
	}

	@GetMapping("/list")
	@ResponseBody
	@ApiOperation(value = "后台分页获取所有角色", notes = "后台分页获取所有角色", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "后台分页获取所有角色", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	//    @PreAuthorize("hasAuthority('sys:role:query')")
	public Results list(PageTableRequest request)
	{
		log.info("RoleController.list(): param ( request = " + request + " )");
		request.countOffset();
		return roleService.getAllRolesByPage(request.getOffset(), request.getLimit());
	}

	@GetMapping(value = "/add")
	//    @PreAuthorize("hasAuthority('sys:role:add')")
	@ApiOperation(value = "后台跳转添加角色页面", notes = "后台跳转添加角色页面", httpMethod = "GET", response = String.class)
	public String addRole(Model model)
	{
		model.addAttribute("sysRole", new SysRole());
		return "backPage/role-add";
	}

	@PostMapping(value = "/add")
	@ResponseBody
	//    @PreAuthorize("hasAuthority('sys:role:add')")
	@ApiOperation(value = "保存新增角色", notes = "保存新增角色", httpMethod = "POST", response = Results.class)
	@OperationLogDetail(detail = "保存新增角色", level = 3, operationUnit = OperationUnit.ADMIN, operationType = OperationType.INSERT)
	public Results saveRole(@RequestBody RoleDto roleDto)
	{
		return roleService.save(roleDto);
	}

	@GetMapping(value = "/edit")
	@ApiOperation(value = "跳转修改角色页面", notes = "跳转修改角色页面", httpMethod = "GET", response = String.class)
	public String editRole(Model model, SysRole role)
	{
		model.addAttribute("sysRole", roleService.getRoleById(role.getId()));
		return "backPage/role-edit";
	}

	@PostMapping(value = "/edit")
	@ResponseBody
	//    @PreAuthorize("hasAuthority('sys:role:edit')")
	@ApiOperation(value = "保存修改的角色", notes = "保存修改的角色", httpMethod = "POST", response = Results.class)
	@OperationLogDetail(detail = "保存修改的角色", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	public Results updateRole(@RequestBody RoleDto roleDto)
	{
		return roleService.update(roleDto);
	}

	@GetMapping(value = "/delete")
	@ResponseBody
	//    @PreAuthorize("hasAuthority('sys:role:del')")
	@ApiOperation(value = "删除角色", notes = "删除角色", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "删除角色", level = 4, operationUnit = OperationUnit.ADMIN, operationType = OperationType.DELETE)
	public Results<SysRole> deleteRole(RoleDto roleDto)
	{
		return roleService.delete(roleDto.getId());
	}

	String pattern = "yyyy-MM-dd";

	//只需要加上下面这段即可，注意不能忘记注解
	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request)
	{
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(pattern), true));
		// CustomDateEditor为自定义日期编辑器
	}

	@GetMapping("/findRoleByFuzzyRoleName")
	@ResponseBody
	@ApiOperation(value = "模糊查询角色", notes = "模糊查询角色", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "模糊查询角色", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	//    @PreAuthorize("hasAuthority('sys:role:query')")
	public Results findRoleByFuzzyRoleName(PageTableRequest requests, String roleName)
	{
		requests.countOffset();
		return roleService.getRoleByFuzzyRoleNamePage(roleName, requests.getOffset(), requests.getLimit());
	}

}
